package com.project.plus.controller;

import com.project.plus.entity.Departments;
import com.project.plus.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName DeptController
 * @Description TODO
 * @Author Invoker
 * @Date 2020/9/27 11:09
 * @Version 1.0
 */
@Controller
public class DeptController {
    
    @Autowired
    private DeptService deptService;
    
//    @Autowired
//    private RedisUtil redisUtil;
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @GetMapping("/getAllDept")
    public List<Departments> getAllDept(){
        return deptService.getAllDept();
    }
    
    @GetMapping("/getByNoDept/{deptNo}")
    @ResponseBody
    public Departments getByNoDept(@PathVariable("deptNo") String deptNo){
        Departments departments = new Departments();
        boolean exist = redisTemplate.hasKey(deptNo);
        if (exist) {
            System.out.println("有缓存");
            departments.setDeptNo((String) redisTemplate.opsForValue().get(deptNo));
        }else{
            System.out.println("没有缓存");
            departments = deptService.getByNoDept(deptNo);
            redisTemplate.opsForValue().set(departments.getDeptNo(),departments.getDeptNo());
            redisTemplate.opsForValue().set(departments.getDeptName(),departments.getDeptName());
        }
        return departments;
    }
    
}
